<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;
use app\validate\CheckBanner;
use think\exception\ValidateException;

// 快递管理
class Express extends BaseController
{
    public function index()
    {
        return View::fetch();
    }




    public function load(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }


        $rows = Db::name('express')
            ->select();


        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => 0,
            'data'=> $rows
        );
        return json($data);
    }



    public function add()
    {
        if(!check_jurisdiction1(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }
        return View::fetch();
    }



    public function save(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return $this->outputAjax('无操作权限');
        }

        $post = $request->post();
        $post = remove_xss($post);

        try {
            $data = array(
                'name'       => $post['name'],
                'logo'         => $post['logo'],
                'add_time'    => time(),
                'update_time' => time(),
            );
            if (!Db::name('express')->insert($data)) {
                return $this->outputAjax('操作失败！');
            }
            return $this->outputAjax('操作成功！',0);
        } catch (\Exception $e) {
            return $this->outputAjax($e->getMessage());
        }
    }


    /*
     * 修改
     */
    public function edit(Request $request)
    {
        if(!check_jurisdiction1(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $id = $request->param('id');
        $inf = Db::name('express')->find($id);
        View::assign('inf',$inf);
        return View::fetch();
    }



    public function modify(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return $this->outputAjax('无操作权限');
        }

        $post = $request->post();
        $post = remove_xss($post);

        try {
            $update = array(
                'id'          => $post['id'],
                'name'       => $post['name'],
                'logo'         => $post['logo'],
                'update_time' => time(),
            );
            if (!Db::name('express')->update($update)) {
                return $this->outputAjax('操作失败！');
            }
            return $this->outputAjax('操作成功！',0);
        } catch (\Exception $e) {
            return $this->outputAjax($e->getMessage());
        }
    }

}